package com.yaoran.userauth.controller;

import com.yaoran.userauth.entity.Organization;
import com.yaoran.userauth.service.OrganizationService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "机构管理")
@RestController
@RequestMapping("/organization")
public class OrganizationController {
    private final static Logger logger = LoggerFactory.getLogger(OrganizationController.class);
    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R organizationAdd(@RequestBody Organization organization){
        organizationService.addOrganization(organization,null);
        return R.ok();
    }

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public R<PageUtils<Organization>> listOrganization(@RequestBody Map<String,String> params){
        logger.info("{}",params);
        logger.debug("debug={}",params);
        Map<String,Object> query = new HashMap<>();
        query.put("page",params.get("page"));
        query.put("limit",params.get("limit"));
        String keyWord = params.get("keyWord");
        PageUtils<Organization> pageUtils = organizationService.listOrganization(new Query(query),keyWord);
        return R.ok(pageUtils);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R updateOrganization(@RequestBody Organization organization){
        organizationService.updateOrganization(organization);
        return  R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R deleteOrganization(@RequestBody Map<String,Long> params){
        organizationService.deleteOrganization(params.get("id"));
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/dels")
    public R deleteManyOrganizations(@RequestBody Map<String, Long[]> params){
        organizationService.deleteManyOrganizations(params.get("ids"));
        return R.ok();
    }

    @ApiOperation("机构详情")
    @GetMapping("/detail")
    public R<Organization> getOrganization(@RequestParam Long id){
        Organization organization = organizationService.getOrganization(id);
        return R.ok(organization);
    }

    @ApiOperation("机构列表，不分页")
    @GetMapping("/listAll")
    public R<List<Organization>> listAllOrganization(){
        List<Organization> list = organizationService.listAllOrganization();
        return R.ok(list);
    }
}
